WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
state_block.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21/*
22 * Macro for defining StateBlocks creators.
23 *
24 * Place a call to this macro inside your class declaration
25 * (in the state_block_class.h file), preferably just after the constructors.
26 *
27 * In order to use this macro, the derived state_block class, StateBlockClass,
28 * must have the constructor callable as:
29 *
30 * StateBlockClass(const Eigen::VectorXd& _state, bool _fixed)
31 *
32 * And the static method:
33 *
34 * static Eigen::VectorXd Identity()
35 */
36#define WOLF_STATE_BLOCK_CREATE(StateBlockClass) \
37 static StateBlockPtr create(const Eigen::VectorXd& _state, bool _fixed) \
38 { \
39 return std::make_shared<StateBlockClass>(_state, _fixed); \
40 } \
41 static StateBlockPtr createIdentity(bool _fixed) \
42 { \
43 return std::make_shared<StateBlockClass>(StateBlockClass::Identity(), _fixed); \
44 }
45
46// Fwd references
47namespace wolf
48{
49class NodeBase;
50class LocalParametrizationBase;
51} // namespace wolf
52
53// Wolf includes
54#include "core/common/wolf.h"
56
57// std includes
58#include <iostream>
59#include <mutex>
60
61namespace wolf
62{
72class StateBlock : public std::enable_shared_from_this<StateBlock>
73{
74 public:
75 protected:
76 std::string type_;
77 std::atomic_bool fixed_;
78
79 std::atomic<SizeEigen> state_size_;
80 Eigen::VectorXd state_;
81 mutable std::mutex mut_state_;
82
83 LocalParametrizationBasePtr
85
86 std::atomic_bool state_updated_;
87 std::atomic_bool fix_updated_;
88 std::atomic_bool
90
92
93 public:
100 StateBlock(const std::string& _type,
101 const SizeEigen _size,
102 bool _fixed = false,
103 LocalParametrizationBasePtr _local_param_ptr = nullptr,
104 bool _transformable = true);
105
112 StateBlock(const std::string& _type,
113 const Eigen::VectorXd& _state,
114 bool _fixed = false,
115 LocalParametrizationBasePtr _local_param_ptr = nullptr,
116 bool _transformable = true);
117
119 StateBlock(const StateBlock& o) = delete;
120 StateBlock(StateBlock&& o) = delete;
121 StateBlock& operator=(const StateBlock& o) = delete;
122
125 virtual ~StateBlock();
126
129 std::string getType() const;
130
133 Eigen::VectorXd getState() const;
134
137 double* getStateData();
138
141 void setState(const Eigen::VectorXd& _state, const bool _notify = true);
142
145 SizeEigen getSize() const;
146
149 SizeEigen getLocalSize() const;
150
153 bool isFixed() const;
154
157 void fix();
158
161 void unfix();
162
165 void setFixed(bool _fixed);
166
169 bool hasLocalParametrization() const;
170
173 LocalParametrizationBasePtr getLocalParametrization() const;
174
177 void setLocalParametrization(LocalParametrizationBasePtr _local_param);
178
182
185 bool stateUpdated() const;
186
189 bool fixUpdated() const;
190
193 bool localParamUpdated() const;
194
197 void resetStateUpdated();
198
201 void resetFixUpdated();
202
206
207 virtual void setIdentity(bool _notify = true);
208 void setZero(bool _notify = true);
209
210 virtual Eigen::VectorXd identity() const;
211 Eigen::VectorXd zero() const;
212
217 void perturb(double amplitude = 0.1);
218
219 bool isTransformable() const
220 {
221 return transformable_;
222 }
223
224 void setTransformable(bool _trf = true)
225 {
226 transformable_ = _trf;
227 }
228
230 {
231 transformable_ = false;
232 }
233
234 virtual void transform(const VectorComposite& _transformation) = 0;
235
236 void plus(const Eigen::VectorXd& _dv);
237
238 bool isValid(double tolerance = Constants::EPS);
239};
240
241} // namespace wolf
242
243// IMPLEMENTATION
246#include "core/problem/problem.h"
247
248namespace wolf
249{
250inline StateBlock::StateBlock(const std::string& _type,
251 const Eigen::VectorXd& _state,
252 bool _fixed,
253 LocalParametrizationBasePtr _local_param_ptr,
254 bool _transformable)
255 : type_(_type),
256 fixed_(_fixed),
257 state_size_(_state.size()),
258 state_(_state),
259 local_param_ptr_(_local_param_ptr),
260 state_updated_(false),
261 fix_updated_(false),
262 local_param_updated_(false),
263 transformable_(_transformable)
264{
265 // std::cout << "constructed +sb" << std::endl;
266}
267
268inline StateBlock::StateBlock(const std::string& _type,
269 const SizeEigen _size,
270 bool _fixed,
271 LocalParametrizationBasePtr _local_param_ptr,
272 bool _transformable)
273 : type_(_type),
274 fixed_(_fixed),
275 state_size_(_size),
276 state_(Eigen::VectorXd::Zero(_size)),
277 local_param_ptr_(_local_param_ptr),
278 state_updated_(false),
279 fix_updated_(false),
280 local_param_updated_(false),
281 transformable_(_transformable)
282{
283 // std::cout << "constructed +sb" << std::endl;
284}
285
287{
288 // std::cout << "destructed -sb" << std::endl;
289}
290
291inline std::string StateBlock::getType() const
292{
293 return type_;
294}
295
296inline Eigen::VectorXd StateBlock::getState() const
297{
298 std::lock_guard<std::mutex> lock(mut_state_);
299 return state_;
300}
301
303{
304 return state_size_.load();
305}
306
308{
309 if (local_param_ptr_) return local_param_ptr_->getLocalSize();
310 return getSize();
311}
312
313inline bool StateBlock::isFixed() const
314{
315 return fixed_.load();
316}
317
318inline void StateBlock::fix()
319{
320 setFixed(true);
321}
322
323inline void StateBlock::unfix()
324{
325 setFixed(false);
326}
327
329{
330 return (local_param_ptr_ != nullptr);
331}
332
333inline LocalParametrizationBasePtr StateBlock::getLocalParametrization() const
334{
335 return local_param_ptr_;
336}
337
339{
340 assert(local_param_ptr_ != nullptr && "state block without local parametrization");
341 local_param_ptr_.reset();
342 local_param_updated_.store(true);
343}
344
345inline void StateBlock::setLocalParametrization(LocalParametrizationBasePtr _local_param)
346{
347 assert(_local_param != nullptr && "setting a null local parametrization");
348 local_param_ptr_ = _local_param;
349 local_param_updated_.store(true);
350}
351
352inline bool StateBlock::stateUpdated() const
353{
354 return state_updated_.load();
355}
356
357inline bool StateBlock::fixUpdated() const
358{
359 return fix_updated_.load();
360}
361
363{
364 return local_param_updated_.load();
365}
366
368{
369 state_updated_.store(false);
370}
371
373{
374 fix_updated_.store(false);
375}
376
378{
379 local_param_updated_.store(false);
380}
381
383{
384 return state_.data();
385}
386
387inline void StateBlock::setIdentity(bool _notify)
388{
389 setState(Eigen::VectorXd::Zero(state_size_), _notify);
390}
391
392inline void StateBlock::setZero(bool _notify)
393{
394 setIdentity(_notify);
395}
396
397inline Eigen::VectorXd StateBlock::identity() const
398{
399 return Eigen::VectorXd::Zero(state_size_);
400}
401
402inline Eigen::VectorXd StateBlock::zero() const
403{
404 return identity();
405}
406
407inline bool StateBlock::isValid(double tolerance)
408{
409 return local_param_ptr_ ? local_param_ptr_->isValid(state_, tolerance) : true;
410}
411
412} // namespace wolf
class StateBlock
Definition: state_block.h:73
StateBlock & operator=(const StateBlock &o)=delete
StateBlock(StateBlock &&o)=delete
void perturb(double amplitude=0.1)
perturb all states states with random noise following an uniform distribution in [ -amplitude,...
Definition: state_block.cpp:47
std::string getType() const
Returns the type of the state block.
Definition: state_block.h:291
bool localParamUpdated() const
Return if local parameterization has been updated.
Definition: state_block.h:362
Eigen::VectorXd zero() const
Definition: state_block.h:402
void resetStateUpdated()
Set state_updated_ to false.
Definition: state_block.h:367
std::atomic_bool local_param_updated_
Flag to indicate whether the local_parameterization has been updated or not.
Definition: state_block.h:89
std::string type_
Type of the derived class.
Definition: state_block.h:76
bool isFixed() const
Returns if the state is fixed (not estimated)
Definition: state_block.h:313
bool isTransformable() const
Definition: state_block.h:219
SizeEigen getSize() const
Returns the state size.
Definition: state_block.h:302
void unfix()
Sets the state as estimated.
Definition: state_block.h:323
virtual void setIdentity(bool _notify=true)
Definition: state_block.h:387
void resetFixUpdated()
Set fix_updated_ to false.
Definition: state_block.h:372
bool transformable_
Flag to indicate if the state block can be transformed to another reference frame.
Definition: state_block.h:91
virtual void transform(const VectorComposite &_transformation)=0
void setNonTransformable()
Definition: state_block.h:229
bool isValid(double tolerance=Constants::EPS)
Definition: state_block.h:407
bool stateUpdated() const
Return if state has been updated.
Definition: state_block.h:352
LocalParametrizationBasePtr getLocalParametrization() const
Returns the state local parametrization ptr.
Definition: state_block.h:333
std::atomic_bool state_updated_
Flag to indicate whether the state has been updated or not.
Definition: state_block.h:86
void plus(const Eigen::VectorXd &_dv)
Definition: state_block.cpp:56
void setState(const Eigen::VectorXd &_state, const bool _notify=true)
Sets the state vector.
Definition: state_block.cpp:25
std::mutex mut_state_
State vector mutex.
Definition: state_block.h:81
Eigen::VectorXd getState() const
Returns the state vector.
Definition: state_block.h:296
StateBlock(const std::string &_type, const SizeEigen _size, bool _fixed=false, LocalParametrizationBasePtr _local_param_ptr=nullptr, bool _transformable=true)
Constructor from size.
Definition: state_block.h:268
void setZero(bool _notify=true)
Definition: state_block.h:392
virtual Eigen::VectorXd identity() const
Definition: state_block.h:397
void fix()
Sets the state as fixed.
Definition: state_block.h:318
SizeEigen getLocalSize() const
Returns the size of the local parametrization.
Definition: state_block.h:307
virtual ~StateBlock()
Destructor.
Definition: state_block.h:286
void setLocalParametrization(LocalParametrizationBasePtr _local_param)
Sets a local parametrization.
Definition: state_block.h:345
StateBlock(const StateBlock &o)=delete
void setTransformable(bool _trf=true)
Definition: state_block.h:224
Eigen::VectorXd state_
State vector storing the state values.
Definition: state_block.h:80
LocalParametrizationBasePtr local_param_ptr_
Local parametrization useful for optimizing in the tangent space to the manifold.
Definition: state_block.h:84
void removeLocalParametrization()
Removes the state_block local parametrization.
Definition: state_block.h:338
bool fixUpdated() const
Return if fix/unfix has been updated.
Definition: state_block.h:357
void setFixed(bool _fixed)
Sets the state status.
Definition: state_block.cpp:38
std::atomic_bool fix_updated_
Flag to indicate whether the status has been updated or not.
Definition: state_block.h:87
void resetLocalParamUpdated()
Set localk_param_updated_ to false.
Definition: state_block.h:377
std::atomic< SizeEigen > state_size_
State vector size.
Definition: state_block.h:79
double * getStateData()
Returns the state vector data array.
Definition: state_block.h:382
std::atomic_bool fixed_
Key to indicate whether the state is fixed or not.
Definition: state_block.h:77
bool hasLocalParametrization() const
Returns if the state has a local parametrization.
Definition: state_block.h:328
Definition: vector_composite.h:27
Namespace extending Eigen definitions.
Definition: wolf.h:94
const double EPS
Definition: wolf.h:74
Definition: capture_base.h:23
EIGEN_DEFAULT_DENSE_INDEX_TYPE SizeEigen
Vector and Matrices size type for the Wolf project.
Definition: wolf.h:58